package cn.cpf.tool.doc.pdf;

import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * <b>Description : </b>
 *
 * @author CPF
 * Date: 2020/5/7 10:04
 */
public class StringUtil {

    public static String StringFilter(String str, String regExp, String replace) {
        String res = "";
        if (str.isEmpty())
            return res;
        Pattern pattern = Pattern.compile(regExp);
        String[] split = pattern.split(str);
        for (String s : split) {
            if (!s.trim().isEmpty())
                res = res + s.trim() + replace;
        }
        int i = res.lastIndexOf(replace);
        if (i != -1)
            res = res.substring(0, i);
        return res;
    }

    public static String[] StringSplit(String str, String regExp) {
        String[] res = new String[0];
        if (str.isEmpty())
            return res;
        Pattern pattern = Pattern.compile(regExp);
        String[] split = pattern.split(str);
        ArrayList<String> arr = new ArrayList<String>();
        for (String s : split) {
            if (!s.trim().isEmpty())
                arr.add(s);
        }
        return arr.<String>toArray(res);
    }

    public static String removeComment(String str, String strComment) {
        if (str.isEmpty())
            return str;
        int i = str.indexOf(strComment);
        if (-1 == i)
            return str;
        return str.substring(0, i);
    }

    public static String acquireNumber(String str) {
        String res = "";
        if (null == str || str.isEmpty())
            return res;
        int start = 0, end = 0;
        boolean bHead = true, bSign = false;
        for (int i = 0; i < str.length(); i++) {
            char ch = str.charAt(i);
            if (bHead) {
                if (ch == '-' || ch == '+') {
                    bSign = true;
                    start = i;
                } else if ('0' <= ch && ch <= '9') {
                    bHead = false;
                    if (!bSign)
                        start = i;
                    end = str.length();
                }
            } else if ('0' > ch || ch > '9') {
                end = i;
                break;
            }
        }
        if (end > start)
            res = str.substring(start, end);
        return res;
    }

    public static String filterNumber(String str, String filter) {
        String res = "";
        if (null == str || str.isEmpty())
            return res;
        int start = 0, end = str.length();
        int i;
        for (i = 0; i < str.length(); ) {
            String s = str.substring(i, i + 1);
            if (filter.contains(s)) {
                i++;
                continue;
            }
            start = i;
        }
        for (i = str.length() - 1; i >= 0; ) {
            String s = str.substring(i, i + 1);
            if (filter.contains(s)) {
                i--;
                continue;
            }
            end = i + 1;
        }
        if (start < end)
            res = str.substring(start, end);
        return res;
    }

    public static boolean isInteger(String input) {
        Matcher mer = Pattern.compile("^[+-]?[0-9]+$").matcher(input);
        return mer.find();
    }
}
